<?php
// src/Blogger/BlogBundle/Controller/CommentController.php

namespace Inter\ArticleClientBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Inter\ArticleBundle\Entity\Comment;
use Inter\ArticleBundle\Form\Type\CommentType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\Security\Acl\Domain\ObjectIdentity;
use Symfony\Component\Security\Acl\Domain\UserSecurityIdentity;
use Symfony\Component\Security\Acl\Permission\MaskBuilder;

/**
 * Comment controller.
 */
class CommentController extends Controller
{
    public function newAction($article_id)
    {
        $article = $this->getArticle($article_id);

        $comment = new Comment();
        $comment->setArticle($article);
        $form   = $this->createForm(new CommentType(), $comment);

        return $this->render('ArticleClientBundle:Comment:form.html.twig', array(
            'comment' => $comment,
            'form'   => $form->createView()
        ));
    }

    public function createAction($article_id)
    {
        $article = $this->getArticle($article_id);

        $comment  = new Comment();
        $comment->setArticle($article);
        $request = $this->getRequest();
        $form    = $this->createForm(new CommentType(), $comment);
        $form->bind($request);

    	if ($form->isValid()) {
            $em = $this->getDoctrine()
                       ->getEntityManager();
            $em->persist($comment);
            $em->flush();

            if (true === $this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY')) 
            {
	            // creating the ACL
	            $aclProvider = $this->get('security.acl.provider');
	            $objectIdentity = ObjectIdentity::fromDomainObject($comment);
	            $acl = $aclProvider->createAcl($objectIdentity);
	            
	            // retrieving the security identity of the currently logged-in user
	            $tokenStorage = $this->get('security.token_storage');
	            $user = $tokenStorage->getToken()->getUser();
	            $securityIdentity = UserSecurityIdentity::fromAccount($user);
	            
	            // grant owner access
	            $acl->insertObjectAce($securityIdentity, MaskBuilder::MASK_OWNER);
	            $aclProvider->updateAcl($acl);
            }
            
            return $this->redirect($this->generateUrl('article_client_article_show', array(
                'id' => $comment->getArticle()->getId(),
            	'slug'  => $comment->getArticle()->getSlug())) .
                '#comment-' . $comment->getId()
            );
        }

        return $this->render('ArticleClientBundle:Comment:create.html.twig', array(
            'comment' => $comment,
            'form'    => $form->createView()
        ));
    }

    protected function getArticle($article_id)
    {
        $em = $this->getDoctrine()
                    ->getEntityManager();

        $article = $em->getRepository('InterArticleBundle:Article')->find($article_id);

        if (!$article) {
            throw $this->createNotFoundException('Unable to find Article post.');
        }

        return $article;
    }    
   
    
    public function editAction(Request $request){
    	
    	$article_id = $request->query->get('article_id', 1);
    	$comment_id = $request->query->get('comment_id', 1);
    	
    	$em = $this->getDoctrine()->getManager();
    	$article = $this->getArticle($article_id);    	
    	$comments = $article->getComments();
    	
    	foreach ($comments as $comment) {
    		if($comment->getId() == $comment_id) {
    			$comment_eidt = $comment;
    			break;
    		}
    	}
    	
    	$authorizationChecker = $this->get('security.authorization_checker');
    	
    	// check for edit access
    	if (false === $authorizationChecker->isGranted('EDIT', $comment_eidt)) {
    		throw new AccessDeniedException();
    	}
    	
    	
    	$form   = $this->createForm(new CommentType(), $comment_eidt);
    	
    	if ($request->getMethod() == 'POST') {
    		$form->bind($request);
    		 
    		if ($form->isValid()) {
    			$em->flush();
    	
    			return $this->redirect($this->generateUrl('article_client_article_show', array(
                'id' => $comment->getArticle()->getId(),
            	'slug'  => $comment->getArticle()->getSlug())) .
                '#comment-' . $comment->getId()
            );
    		}
    	}
    	
    	return $this->render('ArticleClientBundle:Comment:edit.html.twig', array(
    			'comment' => $comment_eidt,
    			'form'   => $form->createView()
    	));
    }
    
    

}